/**
 * Txt Reader can read a Txt file
 */
package cn.penciltim.smstoexcel;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author penciltim
 * 
 */
public class SmsTxtReader {
	public Sms read(File fileName) throws IOException {
		// Reading inputs by lines.
		BufferedReader in = new BufferedReader(new InputStreamReader(
				new FileInputStream(fileName), "unicode"));
		String s;
		String temp[] = new String[3];
		StringBuffer sb = new StringBuffer();
		int indice = 0;
		while ((s = in.readLine()) != null) {
			if (s.equals(""))
				continue;
			if (indice >= 2) {
				sb.append(s);
				continue;
			}
			if (s.contains("15915859370"))
				temp[indice] = "小老公";
			else if (s.contains("13560310461"))
				temp[indice] = "小老婆";
			else
				temp[indice] = s;
			indice++;
		}
		in.close();
		return new Sms(temp[0], temp[1], sb.toString());
	}

	public Sms[] readAll(File path) throws IOException {
		File[] files = path.listFiles();
		Sms[] sms = new Sms[files.length];
		int i = 0;
		for (File f : files) {
			sms[i] = read(f);
			// System.out.println(i);
			i++;
		}
		return sms;
	}
}
